<template>
  <div>
    <el-dialog
      title="自定义校验"
      destroy-on-close
      append-to-body
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="dialogVisible"
      width="60%"
    >
      <el-button @click="innerVisible = true">添加校验</el-button>
      <el-table :data="rules.filter(item=>item.ruleType=='selfadd')" style="width: 100%">
        <el-table-column prop="name" label="校验名称"> </el-table-column>
        <el-table-column prop="code" label="校验编码"> </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button
              @click.native.prevent="editorRow(scope.$index, scope.row)"
              type="text"
              size="small"
            >
              编辑
            </el-button>
            <el-button
              @click.native.prevent="deleteRow(scope.$index, rules)"
              type="text"
              size="small"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog
        width="60%"
        destroy-on-close
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="innerVisible"
        append-to-body
      >
        <el-row>
          <el-col :span="22">
            <el-form
              :model="form"
              :label-position="'left'"
              ref="form"
              label-width="80px"
              class="demo-form"
            >
              <el-form-item
                label="校验名称"
                prop="name"
                :rules="[
                  {
                    required: true,
                    message: '请输入校验名称',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-input v-model="form.name"></el-input>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="2">
            <div class="help-icon-wrap">
              <img src="~@/low-code/assets/help-icon.png" alt="" />
              <span>帮助</span>
            </div>
          </el-col>
        </el-row>

        <editor
          v-model="eventContent"
          @init="editorInit"
          :options="editorOptions"
          lang="javascript"
          theme="chrome"
          width="100%"
          height="350"
        ></editor>
        <span slot="footer" class="dialog-footer">
          <el-button @click="innerVisible = false">取 消</el-button>
          <el-button type="primary" @click="addEvent('form')">确 定</el-button>
        </span>
      </el-dialog>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { ruleCheckOringin } from "@/low-code/utils/enumeration.js";
export default {
  props: ["rules"],
  components: {
    editor: require("vue2-ace-editor"),
  },
  data() {
    return {
      ruleCheckOringin: ruleCheckOringin,
      editorOptions: {
        enableBasicAutocompletion: true, // 启用基本自动完成
        enableSnippets: true, // 启用代码段
        enableLiveAutocompletion: true, // 启用实时自动完成
        printMarginColumn: 30,
        displayIndentGuides: false, // 显示参考线
        enableEmmet: true, // 启用Emmet
        tabSize: 5, // 标签大小
        fontSize: 12, // 设置字号
        useWorker: true, // 使用辅助对象
        showPrintMargin: false, //去除编辑器里的竖线
        enableMultiselect: true, //     选中多处
        readOnly: false, // 是否只读
        showFoldWidgets: true, // 显示折叠部件
        fadeFoldWidgets: true, // 淡入折叠部件
        wrap: true, //换行
      },
      dialogVisible: false,
      innerVisible: false,
      eventContent: `(rule, value, callback,form)=>{
         callback();
        //  callback(new Error("自定义校验失败"));
}`,
      form: {
        name: "",
        keyCode: "",
      },
    };
  },
  methods: {
    editorInit: function () {
      require("brace/ext/language_tools"); //language extension prerequsite...
      require("brace/theme/chrome");
      require("brace/mode/javascript"); //language
      require("brace/snippets/javascript"); //snippet
    },
    random() {
      let len = 8;
      let $chars = "ABCDEFGHJKMNPQRSTWXYZ";
      let maxPos = $chars.length;
      let id = "";
      for (let i = 0; i < len; i++) {
        id += $chars.charAt(Math.floor(Math.random() * maxPos));
      }
      return id;
    },
    addEvent(formName) {
      let _this = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let keyCode = null;
          if (!this.form.keyCode) {
            keyCode = this.random();
            _this.rules.push({
              validator: _this.eventContent,
              trigger: "blur",
              ruleType: ruleCheckOringin.SELFADD,
              name: _this.form.name,
              code: keyCode,
            });
          } else {
            keyCode = this.form.keyCode;
            let findRule = _this.rules.find((item) => {
              return item.code == keyCode;
            });
            Object.assign(findRule, {
              code: keyCode,
              name: this.form.name,
              validator: _this.eventContent,
            });
          }
          this.form.name = "";
          this.form.keyCode = "";
          this.innerVisible = false;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    editorRow(index, rows) {
      this.innerVisible = true;
      this.eventContent = rows.validator;
      this.form.name = rows["name"];
      this.form.keyCode = rows["code"];
    },
    deleteRow(index, rules) {
      rules.splice(index, 1);
    },
  },
  watch: {
    innerVisible(value) {
      if (!value) {
        this.$refs["form"].resetFields();
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.form-config,
.box-model-config {
  padding-left: 10px;
}
.form-config .title,
.box-model-config .title {
  line-height: 32px;
  font-size: 14px;
}
.form-config .el-input-number,
.box-model-config .el-input-number {
  line-height: 30px;
}

.help-icon-wrap {
  display: flex;
  justify-items: center;
  cursor: pointer;
  img {
    width: 30px;
    height: 30px;
    margin-top: 5px;
  }
  span {
    line-height: 39px;
  }
}
.help-document-wrap {
  .title {
    line-height: 38px;
    padding: 0px 10px;
    font-size: 18px;
    position: relative;
    background: #f5f5f5;
  }
  .title::before {
    position: absolute;
    left: 0;
    top: 0;
    content: "";
    width: 3px;
    height: 38px;
    background: #409eff;
  }
  .desc {
    padding: 10px;
  }
}
</style>